package com.common.queueloader.items
{
	import com.common.queueloader.AbstractItem;
	import com.common.queueloader.ILoadable;
	import com.common.queueloader.QueueVo;
	
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 *加载swf文件 item 
	 * @author lake
	 * 
	 */	
	public class SwfItem extends AbstractItem implements ILoadable
	{
		/**
		 *加载上下文 
		 */		
		private var context:LoaderContext;
		
		public function SwfItem(queueVo:QueueVo, path:URLRequest, fileType:int)
		{
			super(queueVo, path, fileType);
		}
		/**
		 * 开始加载
		 * 
		 */		
		override public function load():void
		{
			var applicationDomain:ApplicationDomain=queueVo.applicationDomain;
			if (applicationDomain == null)
			{
				applicationDomain=ApplicationDomain.currentDomain;
			}
			context = new LoaderContext(false, applicationDomain)
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
			_loader.load(path, context);
		}
		/**
		 *重新加载 
		 */		
		override public function reLoad():void
		{
			_loader.load(path,context);
		}
		/**
		 * 
		 * 销毁
		 */		
		override public function dispose():void
		{
			_progress=0;
			_bytesLoaded=0;
			_bytesTotal=0;
			_queueVo=null;
			_path=null;
			context = null;
			_isLoading=false;
			_fileType=0;
			_loader=null;
			_httpStatusFunction=null;
			_errorFunction=null;
			_completeFunction=null;
			_progressFunction=null;
		}
		/**
		 *关闭 
		 * 
		 */		
		override public function close():void
		{
			if (_loader)
			{
				Loader(_loader).unloadAndStop(true);
			}
		}
		/**
		 *加载完成 
		 * @param evt
		 * 
		 */		
		override public function onComplete(evt:Event):void
		{
			_content=evt.target.content;
			_completeFunction(evt);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			this.dispose();
		}
		/**
		 *加载进度 
		 * @param evt
		 * 
		 */		
		override public function onProgress(evt:ProgressEvent):void
		{
			if (_progressFunction == null)
				return;
			_bytesLoaded=evt.bytesLoaded;
			_bytesTotal=evt.bytesTotal;
			_progress=Math.floor(evt.bytesLoaded / evt.bytesTotal * 100);
			_progressFunction(evt.clone());
		}
		/**
		 *状态事件 
		 * @param evt
		 * 
		 */		
		override public function onHttpStatus(evt:HTTPStatusEvent):void
		{
			
		}
		/**
		 *加载出错 
		 * @param evt
		 * 
		 */		
		override public function onError(evt:ErrorEvent):void
		{
			if(_curLoadTimes > _reLoadTimes)
			{
				_errorFunction(evt.clone());
			}
			else
			{
				_curLoadTimes ++;
				reLoad();
			}
		}
	}
}
